package com.ce.util.chart;


import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;

import com.ce.framework.util.StringUtil;

public class ChartData implements Serializable{

	private int hashValue = 0;
	private String title;
	private String value;
	private String color;
	private String url;
	private String desc;
	private HashMap<String,String> otherAttr = new HashMap<String,String>();
	
	public ChartData(String title, String value) {
		super();
		this.title = title;
		this.value = value;
	}

	public ChartData(String title, String value, String color) {
		super();
		this.title = title;
		this.value = value;
		this.color = color;
	}

	public ChartData(String title, String value, String color, String url) {
		super();
		this.title = title;
		this.value = value;
		this.url = url;
		this.color = color;
	}
	
	public ChartData addAttribute(String name,String value){
		otherAttr.put(name, value);
		return this;
	}

	public Element toElement(){
		Element e = new Element("slice",value);
		HashMap<String,String> hm = this.getOtherAttr();
		Iterator<String> it = hm.keySet().iterator();
		String key,val;
		while(it.hasNext()){
			key = it.next();val = hm.get(key);
			e.setAttribute(key, val);
		}
		e.setAttribute("title", title);
		return e;
	}

	public String toString(){
		return this.toElement().toString();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public HashMap<String, String> getOtherAttr() {
		if(StringUtil.isNotBlank(color))
			otherAttr.put("color", color);
		if(StringUtil.isNotBlank(url))
			otherAttr.put("url", url);
		if(StringUtil.isNotBlank(desc))
			otherAttr.put("description", desc);
		return otherAttr;
	}

	public void setOtherAttr(HashMap<String, String> otherAttr) {
		this.otherAttr = otherAttr;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
            return false;
        if (! (obj instanceof ChartData))
            return false;
        ChartData that = (ChartData) obj;
        if (this.getTitle() == null || that.getTitle() == null)
            return false;
        return (this.getTitle().equals(that.getTitle()));
	}

	@Override
	public int hashCode() {
		if (this.hashValue == 0)
        {
            int result = 17;
            int idValue = this.getTitle() == null ? 0 : this.getTitle().hashCode();
            result = result * 37 + idValue;
            this.hashValue = result;
        }
        return this.hashValue;
	}
	
}
